
import { ajax } from 'nc-lightapp-front';
/**
 * 编辑单据时，动态控制往来对象的编辑性，不再通过编辑前事件控制
 * @param {*} props 
 * @param {*} formId 
 * @param {*} tableId 
 */
function objtypeEditable(props, formId, tableId) {
    var billtype = props.form.getFormItemsValue(formId, 'pk_billtype') ? props.form.getFormItemsValue(formId, 'pk_billtype').value : null;
    //往来对象 0=客户，1=供应商，2=部门，3=业务员
    var objtype = props.form.getFormItemsValue(formId, 'objtype') ? props.form.getFormItemsValue(formId, 'objtype').value : null;
    var isrefund = props.form.getFormItemsValue(formId, 'isrefund') ? props.form.getFormItemsValue(formId, 'isrefund').value : null;
    // if (billtype && (billtype == 'F1' || billtype == "23E1")) {
    //     //应付单、付款单往来对象是客户，控制往来对象、币种不可编辑
    //     if (objtype && objtype == "0") {
    //         props.form.setFormItemsDisabled(formId, { 'objtype': true });
    //         props.form.setFormItemsDisabled(formId, { 'pk_currtype': true });
    //         props.cardTable.setColEditableByKey(tableId, ['objtype', 'pk_currtype'], true)
    //     }
    // } else if (billtype && (billtype == 'F0' || billtype == 'F2' || billtype == "23E0")) {
    //     //应收单、收款单往来对象是供应商，控制往来对象、币种不可编辑
    //     if (objtype && objtype == "0") {
    //         props.form.setFormItemsDisabled(formId, { 'objtype': true });
    //         props.form.setFormItemsDisabled(formId, { 'pk_currtype': true });
    //         props.cardTable.setColEditableByKey(tableId, ['objtype', 'pk_currtype'], true)
    //     }
    // } else if ((billtype == 'F1' && isrefund) || (billtype == 'F3' && isrefund)) {
    //     //应付退款单、付款退款单控制客户和往来对象不可编辑
    //     props.form.setFormItemsDisabled(formId, { 'objtype': true, 'customer': true });
    //     props.cardTable.setColEditableByKey(tableId, ['objtype', 'customer'], true)
    // }
    if ((billtype == 'F1' && isrefund) || (billtype == 'F3' && isrefund)) {
        //应付退款单、付款退款单控制客户和往来对象不可编辑
        props.form.setFormItemsDisabled(formId, { 'objtype': true, 'customer': true });
        props.cardTable.setColEditableByKey(tableId, ['objtype', 'customer'], true)
    }
}

/**
 * 编辑单据时，动态控制承付、关联结算信息相关字段的编辑性
 * @param {*} props 
 * @param {*} formId 
 * @param {*} tableId 
 */
function dealCommisionPayField(props, formId, tableId) {
    //是否承付
    var isforce = props.form.getFormItemsValue(formId, 'isforce') ? props.form.getFormItemsValue(formId, 'isforce').value : false;
    var billtype = props.form.getFormItemsValue(formId, 'pk_billtype') ? props.form.getFormItemsValue(formId, 'pk_billtype').value : null;
    let supplier = props.form.getFormItemsValue(formId, 'supplier') ? props.form.getFormItemsValue(formId, 'supplier').value : null
    let customer = props.form.getFormItemsValue(formId, 'customer') ? props.form.getFormItemsValue(formId, 'customer').value : null
    if (billtype != 'F2' && billtype != 'F3') {
        return
    }
    //关联结算信息
    let src = props.getUrlParam('src') == 'settlement';
    if (isforce || src) {
        props.form.setFormItemsDisabled(formId, {
            pk_org: true,
            pk_org_v:true,
            objtype: true,
            supplier: true,
            customer: true,
            pk_currtype: true,
            rate: true,
            payaccount: true,
            recaccount: true,
            money: true,
            local_money: true,
            grouplocal: true,
            grouprate: true,
            globallocal: true,
            globalrate: true,
            commpaybegindate: true,
            commpayenddate: true,
            isforce: true
        });
        props.cardTable.setColEditableByKey(
            tableId,
            [
                'objtype',
                'supplier',
                'customer',
                'pk_currtype',
                'rate',
                'grouprate',
                'globalrate',
                'payaccount',
                'recaccount',
                'commpayer',
                'commpaystatus',
                'commpaytype',
                'money_de',
                'local_money_de',
                'quantity_de',
                'money_bal',
                'local_money_bal',
                'quantity_bal',
                'local_tax_de',
                'notax_de',
                'local_notax_de',
                'groupdebit',
                'grouptax_de',
                'groupnotax_de',
                'globaldebit',
                'globaltax_de',
                'globalnotax_de',
                'quantity_cr',
                'local_money_cr',
                'money_cr',
                'local_tax_cr',
                'notax_cr',
                'local_notax_cr',
                'groupcrebit',
                'grouptax_cre',
                'groupnotax_cre',
                'globalcrebit',
                'globaltax_cre',
                'globalnotax_cre',
                'occupationmny',
                'price',
                'groupbalance',
                'globalbalance',
                'taxprice',
                'caltaxmny',
                'nosubtax'
            ],
            true
            
        );
    }
    if (!supplier && billtype == 'F3') {
        props.form.setFormItemsDisabled(formId, { supplier: false });
        props.cardTable.setColEditableByKey(tableId, ['supplier'], false);
    }
    if (!customer && billtype == 'F2') {
        props.form.setFormItemsDisabled(formId, { "customer": false })
        props.cardTable.setColEditableByKey(tableId, ["customer"], false)
    }
}

/**
 * 付款单拉红字应收特殊字段编辑性
 * @param {*} props 
 * @param {*} formId 
 * @param {*} tableId 
 */
function F0ToF3FieldsEditable(props, formId, tableId) {
    props.form.setFormItemsDisabled(formId, { objtype: true, pk_currtype: true });
    props.cardTable.setColEditableByKey(tableId, ['objtype', 'pk_currtype'], true)
}

/**
 * 付款单拉付款申请特殊字段编辑性
 * @param {*} props 
 * @param {*} formId 
 * @param {*} tableId 
 */
function applyToF3FieldsEditable(props, formId, tableId) {
    props.form.setFormItemsDisabled(formId, { pk_org: true, pk_org_v:true, pk_currtype: true, billdate: true, supplier: true });
    props.cardTable.setColEditableByKey(tableId,
        ['pk_currtype', 'billdate', 'supplier', 'prepay', 'purchaseorder', 'invoiceno', 'contractno', 'quantity_de', 'pu_org', 'pu_deptid', 'pu_psndoc'],
        true)
    //付款银行账户如果不为空，则不可编辑
    var payaccount = props.form.getFormItemsValue(formId, 'payaccount') ? props.form.getFormItemsValue(formId, 'payaccount').value : null;
    if (payaccount) {
        props.form.setFormItemsDisabled(formId, { payaccount: true });
        props.cardTable.setColEditableByKey(tableId, ['payaccount'], true)
    }
}

/**
 * 收付款合同推应收应付字段编辑性控制
 * @param {*} props 
 * @param {*} formId 
 * @param {*} tableId 
 */
function fctToArapFieldEditable(props, formId, tableId) {
    let srcbilltype = props.getUrlParam('srcbilltype')
    if (srcbilltype && (srcbilltype == 'FCT1' || srcbilltype == 'FCT2')) {
        props.form.setFormItemsDisabled(formId, { pk_org: true ,pk_org_v:true});
    }
}
/**
 * 采购订单付款计划的付款单，财务组织不可编辑
 * @param {*} props 
 * @param {*} formId 
 * @param {*} tableId 
 */
function puToArapFieldEditable(props, formId, tableId) {
    let srcbilltype = props.getUrlParam('srcbilltype')
    if (srcbilltype  == '21') {
        props.form.setFormItemsDisabled(formId, { pk_org: true ,pk_org_v:true});
    }
}


/**
 * 单据审批流可编辑字段
 * @param {*} that 
 * @param {*} pk_bill 
 * @param {*} billType 
 * @param {*} appcode 
 */
function billEditProperties(that, pk_bill, billType, appcode) {
    ajax({
        url: '/nccloud/arap/arappub/billCanEditProperties.do',
        data: {
            pk_bill: pk_bill,
            billType: billType,
            appcode: appcode
        },
        success: (res) => {
            if (res.success) {
                that.state.buttonfalg = true;
                that.props.resMetaAfterPkorgEdit();
                if (res.data && res.data.head) {
                    that.props.form.setFormItemsDisabled(that.formId, res.data.head);
                    that.headBack = res.data.headBack
                }
                if (res.data && res.data.body) {
                    that.props.cardTable.setColEditableByKey(that.tableId, res.data.body, true)
                    that.bodyBack = res.data.body
                }
                if (that.headBack && !res.data) {
                    that.props.form.setFormItemsDisabled(that.formId, that.headBack);
                    that.props.cardTable.setColEditableByKey(that.tableId, that.bodyBack, false)
                }
                that.props.form.setFormItemsDisabled(that.formId, { pk_org: true ,pk_org_v:true});
            }
        }
    });
}
/**
 * 资金推收款付款字段编辑性控制
 * @param {*} that 
 */
function cardFieldsEditableFromCM(that){
    let cardData = that.props.createMasterChildData(that.getPagecode(), that.formId, that.tableId);
    let top_billtype = '';
    let billtype = '';
    if (cardData && cardData.body && cardData.body[that.tableId] && cardData.body[that.tableId].rows) {
        let bodyrows = cardData.body[that.tableId].rows;
        top_billtype = bodyrows[0].values.top_billtype.value
        billtype = bodyrows[0].values.pk_billtype.value
    }
    let formFields = []
    let tableFieds = []
    if(top_billtype == "36H1"  && billtype == "F2"){
        formFields=['pk_org','pk_org_v']
        tableFieds=['checktype','checkno_display']
    }
    if(formFields && formFields.length > 0 && tableFieds && tableFieds.length > 0){
        var map = {};
        for (let i = 0; i < formFields.length; i++) {
            var key = formFields[i];
            map[key] = true;
        }
        that.props.beforeUpdatePage(); //打开开关
        that.props.form.setFormItemsDisabled(that.formId, map);
        that.props.cardTable.setColEditableByKey(that.tableId, tableFieds, true);
        that.props.updatePage(that.formId, that.tableId); //关闭开关
    }
}

/**
 * 资产单据（推拉） --》F0应收单,F1应付单
 * 字段编辑性控制
 * @param {*} props 
 * @param {*} formId 
 * @param {*} tableId 
 */
function cardFieldsEditable(that) {
    let cardData = that.props.createMasterChildData(that.getPagecode(), that.formId, that.tableId);
    let top_billtype = '';
    let billtype = '';
    if (cardData && cardData.body && cardData.body[that.tableId] && cardData.body[that.tableId].rows) {
        let bodyrows = cardData.body[that.tableId].rows;
        top_billtype = bodyrows[0].values.top_billtype.value
        billtype = bodyrows[0].values.pk_billtype.value
    }
    let formFields = []
    let tableFieds = []
    if (top_billtype && (top_billtype == "4A2F" || top_billtype == "4BL0" || top_billtype == "4BL3") && billtype == "F0") {
        // 4A2F租出租金结算单（推拉） --》F0应收单
        // 4BL0租出合同维护（推） --》F0应收单
        // 4BL3租出费用维护（推） --》F0应收单
        formFields = ['pk_org', 'pk_fiorg', 'pk_pcorg', 'sett_org', 'pk_org_v', 'pk_fiorg_v', 'pk_pcorg_v', 'sett_org_v', 'outbusitype', 'pk_group', 'creationtime', 'creator',
            'pk_billtype', 'pk_tradetype', 'billclass', 'isinit', 'billdate', 'syscode', 'src_syscode', 'billstatus', 'billmaker', 'pk_busitype', 'pk_psndoc',
            'customer', 'pk_currtype', 'objtype']
        tableFieds = ['pk_org', 'pk_pcorg', 'pk_fiorg', 'sett_org', 'pk_pcorg_v', 'pk_org_v', 'pk_fiorg_v', 'sett_org_v', 'customer',
            'equipmentcode', 'billdate', 'pk_group', 'pk_billtype', 'billclass', 'pk_tradetype', 'busidate', 'objtype',
            'direction', 'pk_currtype', 'pk_psndoc', 'money_de', 'local_money_de', 'money_bal', 'local_money_bal', 'notax_de',
            'local_notax_de', 'price', 'taxprice', 'top_billid', 'top_itemid', 'top_billtype', 'top_tradetype', 'src_tradetype',
            'src_billtype', 'src_billid', 'src_itemid', 'pk_payterm', 'assetpactno', 'groupdebit', 'globaldebit', 'groupbalance', 'globalbalance', 'project']
    } else if (top_billtype && (top_billtype == "4A3F" || top_billtype == "4A3H") && billtype == "F1" ) {
        // 4A3F租入租金结算单（推拉） --》F1应付单
        // 4A3H临时租入租金结算单（推拉） --》F1应付单
        formFields = ['billclass', 'isinit', 'billdate', 'syscode', 'src_syscode', 'billstatus', 'billmaker', 'pk_busitype',
            'pk_psndoc', 'supplier', 'pk_currtype', 'objtype', 'pk_org_v', 'pk_fiorg_v', 'pk_pcorg_v', 'sett_org_v',
            'pk_org', 'pk_pcorg', 'sett_org', 'outbusitype', 'pk_group', 'creationtime', 'creator', 'pk_billtype', 'pk_tradetype']
        tableFieds = ['pk_org', 'pk_fiorg', 'pk_pcorg', 'pk_pcorg_v', 'pk_org_v', 'pk_fiorg_v', 'sett_org_v', 'sett_org',
            'supplier', 'equipmentcode', 'billdate', 'pk_group', 'pk_billtype', 'billclass', 'pk_tradetype', 'busidate',
            'objtype', 'direction', 'pk_currtype', 'pk_psndoc', 'local_money_cr', 'money_cr', 'money_bal',
            'local_money_bal', 'price', 'taxprice', 'top_billid', 'top_itemid', 'top_billtype', 'top_tradetype',
            'src_tradetype', 'src_billtype', 'src_billid', 'src_itemid', 'pk_payterm', 'assetpactno', 'contractno',
            'groupcrebit', 'globalcrebit', 'groupbalance', 'globalbalance', 'project']
    } else if(top_billtype && (top_billtype == "4A18" || top_billtype == "4A23" || top_billtype == "4A24" || top_billtype == "4A49") ){
        //资产处置 4A18 //安装调试 4A24 //资产投保 4A23 //保险索赔 4A49
        formFields = ['pk_currtype','rate']
        tableFieds = ['pk_currtype', 'rate', 'money_de', 'local_money_de','local_notax_cr','notax_de','local_notax_de','money_cr','local_money_cr','local_tax_cr','notax_cr','local_notax_cr']
    }else if(top_billtype&&top_billtype == "4A77"&&billtype=="F0"){
        //周转材租赁结算-》应收     购销合同？----摘要默认结算应收租金不可编辑
        formFields = ['pk_org','pk_pcorg','customer','scomment','pk_currtype','billdate','billmaker','approver','pk_org_v']
        tableFieds = ['pk_currtype','money_de','local_money_de','groupdebit','globaldebit','contractno','assetpactno','scomment']
    }else if(top_billtype&&top_billtype == "4BL9"&&billtype=="F0"){
        //租金结算-》应收    
        formFields = ['pk_org_v','pk_org','pk_pcorg','customer','scomment','pk_currtype','billdate','billmaker','approver']
        tableFieds = ['pk_org_v','pk_org','sett_org','pk_pcorg','pk_currtype','money_bal','customer','equipmentcode','billdate',
                      'money_de','local_money_de','groupdebit','globaldebit','contractno','price ','assetpactno','scomment',
                    'taxprice','objtype','pk_payterm','project','busidate']
    }
    if(formFields && formFields.length > 0 && tableFieds && tableFieds.length > 0){
        var map = {};
        for (let i = 0; i < formFields.length; i++) {
            var key = formFields[i];
            map[key] = true;
        }
        that.props.beforeUpdatePage(); //打开开关
        that.props.form.setFormItemsDisabled(that.formId, map);
        that.props.cardTable.setColEditableByKey(that.tableId, tableFieds, true);
        that.props.updatePage(that.formId, that.tableId); //关闭开关
    }
}

export {
    objtypeEditable, dealCommisionPayField, F0ToF3FieldsEditable,
    applyToF3FieldsEditable, fctToArapFieldEditable, billEditProperties,cardFieldsEditableFromCM,
    cardFieldsEditable,puToArapFieldEditable
}